/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010.

	This file is part of the Maxsi Library.

	Maxsi Library is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Library. If not, see <http://www.gnu.org/licenses/>.

	Maxsi Library
	A powerful Cross-Platform C++ General Purpose Library that allows you to
	efficiently create high-performance and reliable applications.

	MaxsiSHA.cpp
	Library for Secure Hash Algorihm stuff.

******************************************************************************/

#include "MaxsiLibrary.h"

BeginMaxsiNamespace

MESTR* CalculateSHA1(BYTE* Buffer, size_t BufferLen)
{
	CSHA1 sha1;

	sha1.Update((uint8_t*)Buffer, BufferLen);

	sha1.Final();

	MESTR* Result = new MESTR[41];

	if ( Result == NULL ) { return NULL; }

	Result[0] = 0;
	sha1.ReportHashM(Result, CSHA1::REPORT_HEX);

	return Result;
}

MaxsiHandle CreateSHA1Context()
{
	return (MaxsiHandle)new CSHA1;
}

void DeleteSHA1Context(MaxsiHandle Context)
{
	delete (CSHA1*)Context;
}

void ResetSHA1Context(MaxsiHandle Context)
{
	((CSHA1*)Context)->Reset();
}

void UpdateSHA1Context(MaxsiHandle Context, BYTE* Buffer, size_t BufferLen)
{
	((CSHA1*)Context)->Update((uint8_t*)Buffer, BufferLen);
}

char* ReportSHA1ContextA(MaxsiHandle Context)
{
	((CSHA1*)Context)->Final();

	char* Result = new char[41];

	if ( Result == NULL ) { return NULL; }

	Result[0] = 0;
	((CSHA1*)Context)->ReportHashA(Result, CSHA1::REPORT_HEX);

	return Result;
}

wchar_t* ReportSHA1ContextW(MaxsiHandle Context)
{
	((CSHA1*)Context)->Final();

	wchar_t* Result = new wchar_t[41];

	if ( Result == NULL ) { return NULL; }

	Result[0] = 0;
	((CSHA1*)Context)->ReportHashW(Result, CSHA1::REPORT_HEX);

	return Result;
}

EndMaxsiNamespace

